package cn.edu.tsinghua.smxq.mobilecity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UpdateQuoteServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312"); 
		output = response.getWriter(); 
		String userId = request.getParameter("userId");
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		String price = request.getParameter("price");
		String priceDate = "";
		try
		{
			priceDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(request.getParameter("priceDate"));
		}
		catch(Exception e)
		{
			priceDate = request.getParameter("priceDate");
		}
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			sql.execute("UPDATE foodquote SET price='" +price+
					"' WHERE userId='" +userId+
					"' AND cityId='" +cityId+
					"' AND foodId='" +foodId+
					"' AND priceDate='" +priceDate+
					"'");
			
			output.write("success");
			sql.close();
			con.close();
			
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());
			
			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312"); 
		output = response.getWriter(); 
		String userId = request.getParameter("userId");
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		String price = request.getParameter("price");
		String priceDate = "";
		try
		{
			priceDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(request.getParameter("priceDate"));
		}
		catch(Exception e)
		{
			priceDate = request.getParameter("priceDate");
		}
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			sql.execute("UPDATE foodquote SET price='" +price+
					"' WHERE userId='" +userId+
					"' AND cityId='" +cityId+
					"' AND foodId='" +foodId+
					"' AND priceDate='" +priceDate+
					"'");
			
			output.write("success");
			sql.close();
			con.close();
			
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());
			
			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}
	}
}
